package io.renren.modules.erp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import io.renren.modules.erp.entity.MaterialExtend;
import io.renren.modules.erp.service.MaterialExtendService;
import io.renren.modules.erp.vo.MaterialExtendVo4List;
import io.renren.modules.sys.controller.AbstractController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/materialsExtend")
public class MaterialExtendController extends AbstractController {
    private Logger logger = LoggerFactory.getLogger(MaterialExtendController.class);
    @Autowired
    private MaterialExtendService materialExtendService;

    @GetMapping(value = "/getDetailList")
    public R getDetailList(@RequestParam("materialId") Long materialId,
                           HttpServletRequest request)throws Exception {

            List<MaterialExtendVo4List> dataList = new ArrayList<MaterialExtendVo4List>();
            if(materialId!=0) {
                dataList = materialExtendService.getDetailList(materialId);
            }
            JSONObject outer = new JSONObject();
            outer.put("total", dataList.size());
            //存放数据json数组
            JSONArray dataArray = new JSONArray();
            if (null != dataList) {
                for (MaterialExtendVo4List md : dataList) {
                    JSONObject item = new JSONObject();
                    item.put("Id", md.getId());
                    item.put("BarCode", md.getBarCode());
                    item.put("CommodityUnit", md.getCommodityUnit());
                    item.put("PurchaseDecimal", md.getPurchaseDecimal());
                    item.put("CommodityDecimal", md.getCommodityDecimal());
                    item.put("WholesaleDecimal", md.getWholesaleDecimal());
                    item.put("LowDecimal", md.getLowDecimal());
                    dataArray.add(item);
                }
            }
            outer.put("rows", dataArray);
            return R.ok().put("data",outer);
    }

    @SysLog("保存")
    @PostMapping("/save")
    public R save(@RequestBody MaterialExtend materialExtend){
        materialExtendService.save(materialExtend);
        return R.ok();
    }

    @PostMapping(value = "/add")
    public R insert(@RequestBody String beanJson, HttpServletRequest request) throws Exception{
        if(1==materialExtendService.insertMaterialExtend(beanJson, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping(value = "/update")
    public R update(@RequestBody String beanJson,@RequestParam("id")Long id, HttpServletRequest request)throws Exception {
        if(1==materialExtendService.updateMaterialExtend(beanJson, id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/delete")
    public R delete(@RequestParam("id")Long id, HttpServletRequest request)throws Exception {
        if(1==materialExtendService.deleteMaterialExtend(id, request,getUserId().toString())){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/batchDelete")
    public R batchDelete(@RequestParam("ids")String ids, HttpServletRequest request)throws Exception {
        if(1==materialExtendService.batchDeleteMaterialExtendByIds(ids, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/checkIsNameExist")
    public R checkIsNameExist(@RequestParam("id") Long id, @RequestParam("name") String name)throws Exception {
        if(0<materialExtendService.checkIsExist(id, name)){
            return R.ok();
        }else{
            return R.error();
        }
    }
}
